﻿using System.Windows.Forms;
using LinkDownloader.Core;
using System.Drawing;
using LinkDownloader.DownloadManger;

namespace LinkDownloader.Forms
{
    internal partial class frmSingleDownload : Form
    {

        private bool started = false;

        private SingleDownload download = null;
        public SingleDownload DownloadJob
        {
            get { return this.download; }
        }

        public string Link
        {
            set { this.lblLink.Text = value; }
        }

        public string SaveDirectory
        {
            set { this.lblSaveDirectory.Text = value; }
        }

        public frmSingleDownload(SingleDownload download)
        {            

            this.download = download;
            InitializeComponent();

        }

        public void IncrementProgress(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(IncrementProgress), value);
            }
            else
            {
                int i = (int)value;
                this.pgbProgress.Increment(i);
                this.lblProgress.Text = string.Format("{0} %", this.pgbProgress.Value);
                this.UpdateTitleText();
            }
        }

        public void UpdateSize(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateSize), value);
            }
            else
            {
                this.lblSize.Text = string.Format("[{0} / {1}]", Utils.FormatSize(this.download.CompletedSize), Utils.FormatSize(this.download.TotalSize));
            }
        }

        public void UpdateStatus(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateStatus), value);
            }
            else
            {
                this.lblStatus.Text = Utils.FormatStatus(this.download.Status);
                switch (this.download.Status)
                {
                    case DownloadStatus.Completed:
                        this.lblStatus.ForeColor = Color.Green;
                        break;

                    case DownloadStatus.Error:
                    case DownloadStatus.Cancelled:
                        this.lblStatus.ForeColor = Color.Red;
                        this.pgbProgress.ForeColor = Color.Red;
                        break;

                    default:
                        this.lblStatus.ForeColor = Color.Black;
                        break;
                }
                this.UpdateTitleText();
            }
        }

        public void UpdateTransferRate(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateTransferRate), value);
            }
            else
            {
                this.lblRate.Text = string.Format("@ {0:0.00} KB/s", this.download.TransferRate);
                this.lblTime.Text = Utils.FormatTime(this.download.TimeLeft);
            }
        }

        public void UpdateFileName(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateFileName), value);
            }
            else
            {
                int i = this.download.FileName.LastIndexOf('.') + 1;
                string extension = (i >= 0 && i < this.download.FileName.Length-1) ? this.download.FileName.Substring(i, this.download.FileName.Length-i) : "broken";
                this.picFile.Image = Utils.GetFileIcon(extension);
            }
        }

        private void UpdateTitleText()
        {
            this.Text = string.Format("{0}[{1}] ({2} %)", (this.download.FileName.Length > 0) ? this.download.FileName + " " : string.Empty, Utils.FormatStatus(this.download.Status), this.download.Progress);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = true;
            this.Hide();
        }

        private void mnuLinkCopy_Click(object sender, System.EventArgs e)
        {
            Clipboard.SetText(this.lblLink.Text);
        }

        private void btnStart_Click(object sender, System.EventArgs e)
        {
            if (!this.started)
            {
                this.download.StartDownload();
            }
            else
            {
                this.download.PauseDownload();
            }
            this.PerformDownloadStartPause();
        }

        public void PerformDownloadStartPause()
        {
            if (!this.started)
            {
                this.btnStart.Text = "Pause";
                this.btnStop.Enabled = true;
            }
            else
            {
                this.btnStart.Text = "Start";
                this.btnStop.Enabled = false;
            }
            this.started = !this.started;
        }

        private void mnuSave_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
            switch (this.download.Status)
            {

                case DownloadStatus.Completed:
                case DownloadStatus.None:
                    this.mnuSaveCannot.Visible = false;
                    this.mnuSaveChange.Visible = true;
                    break;

                default:
                    this.mnuSaveCannot.Visible = true;
                    this.mnuSaveChange.Visible = false;
                    break;

            }

        }

        private void mnuSaveChange_Click(object sender, System.EventArgs e)
        {
            if (this.download.ChangeSaveDirectory())
            {
                this.lblSaveDirectory.Text = this.download.FileDirectory;
            }
        }
        
        private void lblSaveDirectory_MouseClick(object sender, MouseEventArgs e)
        {
            this.lblSaveDirectory.ContextMenuStrip.Show(this.lblSaveDirectory, e.Location);
        }

        private void lblLink_MouseClick(object sender, MouseEventArgs e)
        {
            this.lblLink.ContextMenuStrip.Show(this.lblLink, e.Location);
        }

        private void btnStop_Click(object sender, System.EventArgs e)
        {
            this.download.CancelDownload();
            this.btnStop.Enabled = false;
        }

    }
}
